package eu.dominicum.ft.pd;

import eu.dominicum.ft.pd.mao.MakeProfessionalCyclistMAO;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * A professional cyclist role.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
public class ProfessionalCyclist extends PersistentDomainObject {

    /**
     * Person.
     */
    private Person person;

    /**
     * Nationality.
     */
    private Country nationality;

    /**
     * Make a ProfessionalCyclist object.
     *
     * @param makeProfessionalCyclistMAO -
     * @return ProfessionalCyclist
     */
    public static ProfessionalCyclist make(final MakeProfessionalCyclistMAO makeProfessionalCyclistMAO) {

        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<MakeProfessionalCyclistMAO>> violations = validator.validate(makeProfessionalCyclistMAO);

        if (!violations.isEmpty()) {
            throw new ValidationException((violations.iterator().next().getMessage()));
        }


        ProfessionalCyclist newProfessionalCyclist = new ProfessionalCyclist();

        newProfessionalCyclist.nationality = makeProfessionalCyclistMAO.getNationality();
        newProfessionalCyclist.person = makeProfessionalCyclistMAO.getPerson();

        return newProfessionalCyclist;
    }

    /**
     * Protected default constructor.
     */
    protected ProfessionalCyclist() {

    }

    /**
     * .
     *
     * @return Person
     */
    public final Person getPerson() {
        return person;
    }

    /**
     * .
     *
     * @return Country
     */
    public final Country getNationality() {
        return nationality;
    }

    /**
     * Change the nationality of this professional cyclist.
     *
     * @param newNationality -
     */
    public final void changeNationality(final Country newNationality) {

        this.nationality = newNationality;
    }
}
